/**
 * Created by jihb on 4/12/16.
 * 公共方法
 */
/**
 *
 * @returns {string}
 */

var loadMask;
/**
 * 获取ext app根目录
 * @returns {string}
 */
function appFolder() {
    return ctx+"/app"
}

/**
 * 获取绝对路径
 * @param url
 * @returns {*}
 */

function getUrl(url) {
    if(!url){
        return '';
    }
    url = Ext.String.trim(url);
    return url.startsWith("http://")||url.startsWith("https://")?url:(ctx+url);
}

/**
 * 显示警告信息
 * @param title
 * @param msg
 * @constructor
 */
function ExtAlert(title,msg) {
    Ext.Msg.alert(title,msg);
}

/**
 * 显示普通信息
 * @param title
 * @param msg
 * @constructor
 */
function EXtInfo(title,msg) {
    Ext.Msg.show({
        title: title,
        msg: msg,
        buttons: Ext.MessageBox.OK,
        multiline: true,
        icon: Ext.MessageBox.INFO
    });
}
/**
 * 显示警告信息
 * @param title
 * @param Msg
 * @constructor
 */
function ExtWarning(title,msg) {
    Ext.Msg.show({
        title: title,
        msg: msg,
        buttons: Ext.MessageBox.OK,
        icon: Ext.MessageBox.WARNING
    });
}
/**
 * 显示错误信息
 * @param title
 * @param msg
 * @constructor
 */
function ExtError(title,msg) {
    Ext.Msg.show({
        title: title,
        msg: msg,
        buttons: Ext.MessageBox.OK,
        icon: Ext.MessageBox.ERROR
    });
}
/**
 * 显示toast信息
 * @param title
 * @param message
 * @param pauseTime
 * @constructor
 */
function ExtToast(title, message,pauseTime) {
    if (!pauseTime){
        pauseTime = 2000;
    }
    Ext.MsgTip.msg(title,message,true,pauseTime);
}

/**
 * 创建loader
 * @param options
 * @param callback
 * @returns {Ext.ComponentLoader}
 * @constructor
 */
function ExtCreateLoader(options,callback,returJson) {
    if (!options){
        options = {};
    }
    options.success = function (loader,response,options) {
        if(callback){
            if(returJson){
                callback(Ext.decode(response.responseText));
            }else {
                callback(response.responseText);
            }
        }
    }
    if (returJson){
        options.renderer = 'data'
    }
    options.scripts=true;
    var loader = Ext.create('Ext.ComponentLoader',options);
    return loader;
}

/**
 * 获取grid选中的第一条记录
 * @param grid
 * @returns {*}
 * @constructor
 */
function ExtGetSelectRow(grid) {
    return grid.getSelectionModel().getSelection()[0];
}
/**
 * 获取grid选中的所有记录
 * @param grid
 * @returns {Selection|*}
 * @constructor
 */
function ExtGetSelectRows(grid) {
    return grid.getSelectionModel().getSelection();
}

/**
 * 显示loadMask
 * @param tipMsg
 * @param target
 */
function showLoadMask(tipMsg,target) {
    if(!loadMask){
        loadMask = new Ext.LoadMask(target, {msg:tipMsg});
    }else {
        loadMask.target = target;
        loadMask.msg = tipMsg;
    }
    loadMask.show();
}
/**
 * 隐藏loadMask
 */
function hideLoadMask() {
    if(loadMask){
        loadMask.hide();
    }
}

function ExtSubmitForm(form,url,callback) {
    if(!form){
        ExtError('','表单对象不存在');
        return;
    }
    showLoadMask('处理中...',form);
    var fields = form.getForm().getFields( );

    var canSubmit = true;
    fields.each(function (item,index,len) {
        var allowBlank = item.allowBlank;
        if (allowBlank==false && item.disabled==false && (!item.getValue() || item.getValue()=="")){
            canSubmit = false;
            return false;
        }
    });
    if (canSubmit == false){
        hideLoadMask();
        ExtWarning('','表单信息未填写完整');
        return;
    }
    form.submit({
        clientValidation:true,
        url:url,
        success:function (form, action) {
            console.log(action);
            if(callback){
                callback(action);
            }
        },
        failure:function (form, action) {
            if(action.result){
                ExtError('',action.result.message);
            }
        }
    });
}

/**
 * ajax提交数据
 * @param options {url: 'page.php',params: {id: 1},}
 * @param callback
 * @constructor
 */
function ExtAjaxSubmit(options,callback) {
    if(!options){
        options={};
    }
    if(!options.method){
        options.method = 'POST';
    }
    options.success=function (response) {
        var result = response.responseText;
        var resultJson=Ext.decode(result);
        if(!result || resultJson.code != 0){
            ExtAlert('',resultJson.message);
            return;
        }
        if(callback){
            callback(resultJson);
        }
    }
    Ext.Ajax.request(options);
}


/**
 * 请求发送前触发
 * @param conn Ext.data.Connection
 * @param options Object
 * @param eOpts Object
 * @constructor
 */
function ExtBeforerequest(conn, options, eOpts) {
    console.log("ExtBeforerequest");
}
/**
 * 请求成功放回后触发
 * @param conn
 * @param response
 * @param options
 * @param eOpts
 * @constructor
 */
function ExtRequestcomplete( conn, response, options, eOpts) {
    hideLoadMask();
    var status = response.status;
    var statusText = response.statusText;

    if(response.getResponseHeader("needLogin")){
        parent.window.location.href = getUrl("/index");
    }

}
/**
 * 请求出错(请求已超时或已被中止)
 * @param conn
 * @param response
 * @param options
 * @param eOpts
 * @constructor
 */
function ExtRequestexception(conn, response, options, eOpts) {
    hideLoadMask();
    var status = response.status;
    var statusText = response.statusText;
    if (status==404){
        ExtError('','无法访问服务器');
        return false;
    }
    if (status ==  500){
        ExtError('','服务器处理异常');
        return false;
    }
    if(options.masktarget){
        showLoadMask('处理中...',options.masktarget);
    }
    var result = response.result;
    if(result && result.code != 0){
        ExtError('',result.message);
        return false;
    }
}



//绑定Ajax事件
Ext.Ajax.on("beforerequest",ExtBeforerequest);
Ext.Ajax.on("requestcomplete",ExtRequestcomplete);
Ext.Ajax.on("requestexception",ExtRequestexception);
Ext.Ajax.timeout = 60000; // 超时时间60秒